﻿#include "CSprite.h"
#include "trace.h"

#pragma region :==Cons & Des==:

/* thông tin hàm
// Chức năng: Hàm khởi tạo dành cho IM( Image Manager)
// ID: xác định ID của sprite trong IM
// _TotalFrame: tổng số frame của sprite hiện tại
// SpritePerRow: số sprite có trong một hàng
// FrameWidth: độ rộng của một frame
// FrameHeight: độ cao của một frame
*/
CSprite::CSprite(IDResource ID, int _TotalFrame, int SpritePerRow, int FrameWidth, int FrameHeight) 
{
	_ID = ID;
	_Image = NULL;
	_SpriteHandler = NULL;
	_Width = FrameWidth;
	_Height = FrameHeight;
	_TotalFrame = _TotalFrame;
	_SpritePerRow = SpritePerRow;
	_CurFrame = 0;
	_Position = D3DXVECTOR2::D3DXVECTOR2(0,0);
	_Scale = D3DXVECTOR2::D3DXVECTOR2(1,1);
	_Rotation = D3DXVECTOR2::D3DXVECTOR2((float)_Width/2, (float)_Height/2);
	_Depth = 1.0f;
	_Angle = 0.0f;
	_Color = D3DCOLOR_XRGB(255,255,255);
	_SrcRect.left = _SrcRect.top = 0;
	_SrcRect.right = _Width;
	_SrcRect.bottom = _Height;
}

CSprite::CSprite()
{
	_Image = NULL;
	_SpriteHandler = NULL;
	_Width = 0;
	_Height = 0;
	_TotalFrame = 0;
	_Position = D3DXVECTOR2::D3DXVECTOR2(0,0);
	_Rotation = D3DXVECTOR2::D3DXVECTOR2(0,0);
	_Scale = D3DXVECTOR2::D3DXVECTOR2(1,1);
	_Depth = 1.0f;
	_Angle = 0.0f;
	_Color = D3DCOLOR_XRGB(255,255,255);
	_SpritePerRow = 0;
	_CurFrame = 0;
	_SrcRect.left = _SrcRect.top = 0;
	_SrcRect.right = _Width;
	_SrcRect.bottom = _Height;
}

/* thông tin hàm
// Chức năng: Hàm Copy dùng để copy các sprite trong IM ra
// _Copy: sprite trong IM để copy ra ngoài
*/
CSprite::CSprite(const CSprite &_Copy)
{
	_Image = _Copy._Image;
	_SpriteHandler = _Copy._SpriteHandler;
	_Width = _Copy._Width;
	_Height = _Copy._Height;
	_TotalFrame = _Copy._TotalFrame;
	_Position = _Copy._Position;
	_Scale = _Copy._Scale;
	_Depth = _Copy._Depth;
	_Rotation = _Copy._Rotation;
	_Angle = _Copy._Angle;
	_Color = _Copy._Color;
	_SpritePerRow = _Copy._SpritePerRow;
	_CurFrame = _Copy._CurFrame;
	_Rotation = _Copy._Rotation;
	_SrcRect = _Copy._SrcRect;
}

CSprite::~CSprite()
{
}

#pragma endregion  -- Hàm tạo và hàm hủy

#pragma region :==Update==:
void CSprite::Update(int CurFrame)
{
	// Lấy khung Frame hiện tại

	_SrcRect.left = (CurFrame % _SpritePerRow)*(_Width);
	_SrcRect.top = (CurFrame / _SpritePerRow)*(_Height);
	_SrcRect.right = _SrcRect.left + _Width ;
	_SrcRect.bottom = _SrcRect.top + _Height ;
}

#pragma endregion      -- Cập nhật

#pragma region :==Render==:

// Hàm render 
void CSprite::Render(LPD3DXSPRITE _SpriteHandler,float X, float Y)
{
	_SrcRect.left = (_CurFrame % _SpritePerRow)*(_Width);
	_SrcRect.top = (_CurFrame / _SpritePerRow)*(_Height);
	_SrcRect.right = _SrcRect.left + _Width ;
	_SrcRect.bottom = _SrcRect.top + _Height ;

	_Position.x = X;
	_Position.y = Y;
	// chuyển ma trận

	D3DXMATRIX mTransform;
	D3DXMatrixTransformation2D(&mTransform, NULL, 0, &D3DXVECTOR2(1, 1), &D3DXVECTOR2(0, 0) ,0 , &_Position);
	_SpriteHandler->SetTransform(&mTransform);

	//vẽ hình
	_SpriteHandler->Draw(_Image, &_SrcRect, NULL, &D3DXVECTOR3(0,0,_Depth), _Color);
}

void CSprite::Render(LPD3DXSPRITE SpriteHandler)
{
	D3DXMATRIX mTransform;
		
	D3DXMatrixTransformation2D(&mTransform,NULL,0,&_Scale,&_Rotation,_Angle,&_Position);
	_SpriteHandler->GetTransform(&mTransform);
	SpriteHandler->SetTransform(&mTransform);
	SpriteHandler->Draw(_Image, &_SrcRect, NULL, &D3DXVECTOR3(0,0,_Depth), _Color);
}

#pragma endregion      -- vẽ hình

#pragma region :==Load Image==:

void CSprite::Init(LPDIRECT3DDEVICE9 d3ddv, LPCSTR FilePath, int FrameStart, int FrameEnd, float TimeAnimation)
{
	_Image = LoadImage(d3ddv,FilePath);
}

LPDIRECT3DTEXTURE9 CSprite::LoadImage(LPDIRECT3DDEVICE9 d3ddv, LPCSTR FilePath)
{
	LPDIRECT3DTEXTURE9 _Image;
	D3DXIMAGE_INFO info;
	HRESULT result;
	
	result = D3DXGetImageInfoFromFile(FilePath,&info);
	if (result!=D3D_OK)
	{
		trace(L"[ERROR] Failed to get information from image file '%s'",FilePath);
		return NULL;
	}
 
	result = D3DXCreateTextureFromFileEx(
		d3ddv,
		FilePath,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		&info,
		NULL,
		&_Image);

	if (result!=D3D_OK) 
	{
		trace(L"[ERROR] Failed to create texture from file '%s'",FilePath);
		return NULL;
	}
	return _Image;
}

#pragma endregion  -- tải hình

#pragma region :==Sub Func==:

RECT CSprite::getBoundingBox()
{
	RECT Temp;

	Temp.left = (long)_Position.x;
	Temp.right = (long)Temp.left + _Width;
	Temp.top = (long)_Position.y;
	Temp.bottom = (long)Temp.top + _Height;

	return Temp;
}
#pragma endregion    -- các hàm phụ